package org.ims.inv.service.card.impl;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.ims.inv.dao.card.DAOBusinessCard;
import org.ims.inv.model.card.ModelBusinessCard;
import org.ims.inv.service.card.ServiceBusinessCard;

import cn.trymore.core.exception.ServiceException;
import cn.trymore.core.service.impl.ServiceGenericImpl;
import cn.trymore.core.util.UtilString;

/**
 * Service: 企业名片
 * 
 * @author x36zhao
 *
 */
public class ServiceBusinessCardImpl
extends ServiceGenericImpl<ModelBusinessCard> implements ServiceBusinessCard
{
	/**
	 * The repository of business card
	 */
	private DAOBusinessCard daoBusinessCard;
	
	/**
	 * The sole constructor
	 * 
	 * @param dao
	 */
	public ServiceBusinessCardImpl(DAOBusinessCard dao)
	{
		super(dao);
		this.daoBusinessCard = dao;
	}
	
	/*
	 * (non-Javadoc)
	 * @see org.ims.inv.service.card.ServiceBusinessCard#findCardByName(java.lang.String)
	 */
	@Override
	public List<ModelBusinessCard> findCardByName (String companyName) throws ServiceException
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelBusinessCard.class);
		criteria.add(Restrictions.eq("companyName", companyName));
		return this.getByCriterion(criteria);
	}
	
	/*
	 * (non-Javadoc)
	 * @see cn.trymore.core.service.impl.ServiceGenericImpl#getCriterias(cn.trymore.core.model.ModelBase)
	 */
	@Override
	protected DetachedCriteria getCriterias(ModelBusinessCard domain)
	{
		DetachedCriteria criteria = DetachedCriteria.forClass(ModelBusinessCard.class);
		if (domain != null)
		{
			if (UtilString.isNotEmpty(domain.getCompanyName()))
			{
				criteria.add(Restrictions.like("companyName", domain.getCompanyName(), MatchMode.ANYWHERE));
			}
			
			if (UtilString.isNotEmpty(domain.getContactPerson()))
			{
				criteria.add(Restrictions.like("contactPerson", domain.getContactPerson(), MatchMode.ANYWHERE));
			}
			
			if (UtilString.isNotEmpty(domain.getProjectNo()))
			{
				criteria.add(Restrictions.like("projectNo", domain.getProjectNo(), MatchMode.ANYWHERE));
			}
			
			if (domain.getIndustry() != null)
			{
				criteria.createCriteria("industry").add(Restrictions.eq("id", domain.getIndustry().getId()));
			}
		}
		
		return criteria;
	}

	public DAOBusinessCard getDaoBusinessCard()
	{
		return daoBusinessCard;
	}

	public void setDaoBusinessCard(DAOBusinessCard daoBusinessCard)
	{
		this.daoBusinessCard = daoBusinessCard;
	}
	
}
